package cn.szzsi.common.model;

import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.base.BaseFriendlink;
import com.jfinal.kit.PathKit;

import java.io.File;
import java.util.List;

/**
 * 实体类 - 友情链接
 * 
 */
public class Friendlink extends BaseFriendlink<Friendlink> {

	private static final long serialVersionUID = 2937890826595747504L;

	public static final Friendlink dao = new Friendlink().dao();
	
	/**
	 * 获取所有图片友情链接集合;
	 *
	 * @return 图片友情链接集合
	 *
	 */
	public List<Friendlink> getPictureFriendLinkList() {
		String sql = "select * from mall_friendlink where logo is not null and uid='"+ CommonUtil.PLATFROM_FLAG+"' order by orderList asc ,createDate desc";
		return dao.find(sql);
	}

	/**
	 * 获取该商户所有图片友情链接集合;
	 *
	 * @return 图片友情链接集合
	 *
	 */
	public List<Friendlink> getBusinessPictureFriendLinkList(String uid) {
		String sql = "select * from mall_friendlink where uid=? and logo is not null order by orderList asc ,createDate desc";
		return dao.find(sql,uid);
	}
	
	/**
	 * 获取所有文字友情链接集合;
	 * 
	 * @return 图片友情链接集合
	 * 
	 */
	public List<Friendlink> getTextFriendLinkList() {
		String sql = "select * from mall_friendlink where logo is null and uid='"+ CommonUtil.PLATFROM_FLAG+"' order by orderList asc ,createDate desc";
		return dao.find(sql);
	}

	/**
	 * 获取该商户所有文字友情链接集合;
	 *
	 * @return 图片友情链接集合
	 *
	 */
	public List<Friendlink> getBusinessTextFriendLinkList(String uid) {
		String sql = "select * from mall_friendlink where logo is null and uid=? order by orderList asc ,createDate desc";
		return dao.find(sql,uid);
	}
	
	// 重写方法，删除同时删除Logo文件
	public boolean delete(String id){
		Friendlink persistent = dao.findById(id);
		if (persistent.getStr("logo") != null) {
			File logoFile = new File(PathKit.getWebRootPath() + persistent.getStr("logo"));
			if (logoFile.exists()) {
				logoFile.delete();
			}
		}		
		return dao.deleteById(id);
	}
	
	// 所有友情链接
	public List<Friendlink> getAll(){
		return dao.find("select * from mall_friendlink where uid='"+ CommonUtil.PLATFROM_FLAG+"' order by orderList asc ,createDate desc");
	}

	// 该商户友情链接
	public List<Friendlink> getBusinessAll(String uid){
		return dao.find("select * from mall_friendlink where uid=? order by orderList asc ,createDate desc",uid);
	}
}
